package com.typhoon.spring_boot_demo1.controller;

import com.typhoon.spring_boot_demo1.dto.SearchResultDto;
import com.typhoon.spring_boot_demo1.manager.SearchManager;
import com.typhoon.spring_boot_demo1.request.Operator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 通用搜索控制器
 *
 */
@Controller
@RequestMapping("common")
public class SearchController extends BaseController {

    @Autowired
    private SearchManager searchManager;

    /**
     * 通用查询
     * @param request
     * @param response
     */
    @RequestMapping(value = "/get_search",method = RequestMethod.GET)
    public void commonSearch(HttpServletRequest request, HttpServletResponse response) {
        String key = request.getParameter("key");
        Operator operator = getOperator(request);
        try {
            List<SearchResultDto> list =  this.searchManager.search(key,operator);
            if(null == list || list.isEmpty()) {
                this.putFailtResult(response,"没有查到结果");
                return;
            }
            this.putSuccessResult(response,list);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}
